package cn.yunhe.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
	
	private Map<Integer, ShoppingCartItem> products = new HashMap<>();
	

	public void updateItemQuantity(Integer productid, int quantity){
		ShoppingCartItem sci =products.get(productid);
		if(sci != null){
			sci.setQuantity(quantity);
		}
	}
	

	public void removeItem(Integer productid){
		products.remove(productid);
	}
	

	public void clear(){
		products.clear();
	}
	

	public boolean isEmpty(){
		return products.isEmpty();
	}
	

	public double getTotalMoney(){
		float total = 0;
		
		for(ShoppingCartItem sci: getItems()){
			total += sci.getItemMoney();
		}
		
		return total;
	}
	

	public Collection<ShoppingCartItem> getItems(){
		return products.values();
	}
	

	public int getProductNumber(){
		int total = 0;
		
		for(ShoppingCartItem sci: products.values()){
			total += sci.getQuantity();
		}
		
		return total;
	}
	
	public Map<Integer, ShoppingCartItem> getProducts() {
		return products;
	}
	

	public void addProduct(Product product){
		ShoppingCartItem sci = products.get(product.getProductid());
		
		if(sci == null){
			sci = new ShoppingCartItem(product);
			products.put(product.getProductid(), sci);
		}else{
			sci.increment();
		}
	}
}
